#!/bin/sh

# Slackware build script for suitesparse

# Written by Pablo Santamaria (pablosantamaria@gmail.com)

PRGNAM=suitesparse
VERSION=${VERSION:-3.4.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
    arm*) ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

amd_version=2.2.0
amd_version_major=2
btf_version=1.1.0
btf_version_major=1
camd_version=2.2.0
camd_version_major=2
ccolamd_version=2.7.1
ccolamd_version_major=2
cholmod_version=1.7.1
cholmod_version_major=1
colamd_version=2.7.1
colamd_version_major=2
csparse_version=2.2.3
csparse_version_major=2
cxsparse_version=2.2.3
cxsparse_version_major=2
klu_version=1.1.0
klu_version_major=1
ldl_version=2.0.1
ldl_version_major=2
umfpack_version=5.4.0
umfpack_version_major=5
spqr_version=1.1.2
spqr_version_major=1

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf SuiteSparse
tar xvf $CWD/SuiteSparse-$VERSION.tar.gz

cd SuiteSparse
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

# Set common configuration for the SuiteSparse
cp $CWD/UFconfig.mk UFconfig

mkdir -p Doc/{AMD,BTF,CAMD,CCOLAMD,CHOLMOD,COLAMD,KLU,LDL,UMFPACK,SPQR} Lib Include

pushd AMD
  pushd Lib
    make CFLAGS="$SLKCFLAGS -fPIC"
  popd
  pushd ../Lib
    gcc -shared -Wl,-soname,libamd.so.${amd_version_major} -o \
        libamd.so.${amd_version} ../AMD/Lib/*.o -lm
    ln -sf libamd.so.${amd_version} libamd.so.${amd_version_major}
    ln -sf libamd.so.${amd_version} libamd.so
    cp -p ../AMD/Lib/*.a ./
  popd
  cp -p Include/*.h ../Include
  cp -p README.txt Doc/License Doc/ChangeLog Doc/*.pdf ../Doc/AMD
popd

pushd BTF
  pushd Lib
    make CFLAGS="$SLKCFLAGS -fPIC"
  popd
  pushd ../Lib
    gcc -shared -Wl,-soname,libbtf.so.${btf_version_major} -o \
        libbtf.so.${btf_version} ../BTF/Lib/*.o
    ln -sf libbtf.so.${btf_version} libbtf.so.${btf_version_major}
    ln -sf libbtf.so.${btf_version} libbtf.so
    cp -p ../BTF/Lib/*.a ./
  popd
  cp -p Include/*.h ../Include
  cp -p README.txt Doc/* ../Doc/BTF
popd

pushd CAMD
  pushd Lib
    make CFLAGS="$SLKCFLAGS -fPIC"
  popd
  pushd ../Lib
    gcc -shared -Wl,-soname,libcamd.so.${camd_version_major} -o \
        libcamd.so.${camd_version} ../CAMD/Lib/*.o -lm
    ln -sf libcamd.so.${camd_version} libcamd.so.${camd_version_major}
    ln -sf libcamd.so.${camd_version} libcamd.so
    cp -p ../CAMD/Lib/*.a ./
  popd
  cp -p Include/*.h ../Include
  cp -p README.txt Doc/ChangeLog Doc/License Doc/*.pdf ../Doc/CAMD
popd

pushd CCOLAMD
  pushd Lib
    make CFLAGS="$SLKCFLAGS -fPIC"
  popd
  pushd ../Lib
    gcc -shared -Wl,-soname,libccolamd.so.${ccolamd_version_major} -o \
        libccolamd.so.${ccolamd_version} ../CCOLAMD/Lib/*.o -lm
    ln -sf libccolamd.so.${ccolamd_version} libccolamd.so.${ccolamd_version_major}
    ln -sf libccolamd.so.${ccolamd_version} libccolamd.so
    cp -p ../CCOLAMD/Lib/*.a ./
  popd
  cp -p Include/*.h ../Include
  cp -p README.txt Doc/* ../Doc/CCOLAMD
popd

pushd COLAMD
  pushd Lib
    make CFLAGS="$SLKCFLAGS -fPIC"
  popd
  pushd ../Lib
    gcc -shared -Wl,-soname,libcolamd.so.${colamd_version_major} -o \
        libcolamd.so.${colamd_version} ../COLAMD/Lib/*.o -lm
    ln -sf libcolamd.so.${colamd_version} libcolamd.so.${colamd_version_major}
    ln -sf libcolamd.so.${colamd_version} libcolamd.so
    cp -p ../COLAMD/Lib/*.a ./
  popd
  cp -p Include/*.h ../Include
  cp -p README.txt Doc/* ../Doc/COLAMD
popd

# CHOLMOD can also be compiled to use the METIS library, but it is not
# used here because its licensing terms exclude it from Octave.
pushd CHOLMOD
  pushd Lib
    make CFLAGS="$SLKCFLAGS -fPIC"
  popd
  pushd ../Lib
    gcc -shared -Wl,-soname,libcholmod.so.${cholmod_version_major} -o \
        libcholmod.so.${cholmod_version} ../CHOLMOD/Lib/*.o \
        -lblas -lgfortran -lgfortranbegin -llapack \
        libamd.so.${amd_version_major} \
        libcamd.so.${camd_version_major} libcolamd.so.${colamd_version_major} \
        libccolamd.so.${ccolamd_version_major} -lm
    ln -sf libcholmod.so.${cholmod_version} libcholmod.so.${cholmod_version_major}
    ln -sf libcholmod.so.${cholmod_version} libcholmod.so
    cp -p ../CHOLMOD/Lib/*.a ./
  popd
  cp -p Include/*.h ../Include
  cp -p README.txt Doc/*.pdf ../Doc/CHOLMOD
  cp -p Cholesky/License.txt ../Doc/CHOLMOD/Cholesky_License.txt
  cp -p Core/License.txt ../Doc/CHOLMOD/Core_License.txt
  cp -p MatrixOps/License.txt ../Doc/CHOLMOD/MatrixOps_License.txt
  cp -p Partition/License.txt ../Doc/CHOLMOD/Partition_License.txt
  cp -p Supernodal/License.txt ../Doc/CHOLMOD/Supernodal_License.txt
popd

# CXSparse is a superset of CSparse, and the two share common header
# names, so it does not make sense to build both.
pushd CXSparse
  pushd Lib
    make CFLAGS="$SLKCFLAGS -fPIC"
  popd
  pushd ../Lib
    gcc -shared -Wl,-soname,libcxsparse.so.${cxsparse_version_major} -o \
        libcxsparse.so.${cxsparse_version} ../CXSparse/Lib/*.o -lm
    ln -sf libcxsparse.so.${cxsparse_version} libcxsparse.so.${cxsparse_version_major}
    ln -sf libcxsparse.so.${cxsparse_version} libcxsparse.so
    cp -p ../CXSparse/Lib/*.a ./
  popd
  cp -p Include/cs.h ../Include
  mkdir ../Doc/CXSparse/
  cp -p Doc/* ../Doc/CXSparse
popd

pushd KLU
  pushd Lib
    make CFLAGS="$SLKCFLAGS -fPIC"
  popd
  pushd ../Lib
    gcc -shared -Wl,-soname,libklu.so.${klu_version_major} -o \
        libklu.so.${klu_version} ../KLU/Lib/*.o \
        libamd.so.${amd_version_major} libcolamd.so.${colamd_version_major} \
        libbtf.so.${btf_version_major} libcholmod.so.${cholmod_version_major}
    ln -sf libklu.so.${klu_version} libklu.so.${klu_version_major}
    ln -sf libklu.so.${klu_version} libklu.so
    cp -p ../KLU/Lib/*.a ./
  popd
  cp -p Include/*.h ../Include
  cp -p README.txt Doc/lesser.txt ../Doc/KLU
popd

pushd LDL
  pushd Lib
    make CFLAGS="$SLKCFLAGS -fPIC"
  popd
  pushd ../Lib
    gcc -shared -Wl,-soname,libldl.so.${ldl_version_major} -o \
        libldl.so.${ldl_version} ../LDL/Lib/*.o
    ln -sf libldl.so.${ldl_version} libldl.so.${ldl_version_major}
    ln -sf libldl.so.${ldl_version} libldl.so
    cp -p ../LDL/Lib/*.a ./
  popd
  cp -p Include/*.h ../Include
  cp -p README.txt Doc/ChangeLog Doc/lesser.txt Doc/*.pdf ../Doc/LDL
popd

pushd UMFPACK
  pushd Lib
    make CFLAGS="$SLKCFLAGS -fPIC"
  popd
  pushd ../Lib
    gcc -shared -Wl,-soname,libumfpack.so.${umfpack_version_major} -o \
        libumfpack.so.${umfpack_version} ../UMFPACK/Lib/*.o \
        -lblas -lgfortran -lgfortranbegin -llapack \
        libamd.so.${amd_version_major} -lm
    ln -sf libumfpack.so.${umfpack_version} libumfpack.so.${umfpack_version_major}
    ln -sf libumfpack.so.${umfpack_version} libumfpack.so
    cp -p ../UMFPACK/Lib/*.a ./
  popd
  cp -p Include/*.h ../Include
  cp -p README.txt Doc/License Doc/ChangeLog Doc/gpl.txt Doc/*.pdf ../Doc/UMFPACK
popd

pushd SPQR
  pushd Lib
    make CFLAGS="$SLKCFLAGS -fPIC"
  popd
  pushd ../Lib
    g++ -shared -Wl,-soname,libspqr.so.${spqr_version_major} -o \
        libspqr.so.${spqr_version} ../SPQR/Lib/*.o \
         -lblas -lgfortran -lgfortranbegin -llapack \
        libcholmod.so.${cholmod_version_major} -lm
    ln -sf libspqr.so.${spqr_version} libspqr.so.${spqr_version_major}
    ln -sf libspqr.so.${spqr_version} libspqr.so
    cp -p ../SPQR/Lib/*.a ./
  popd
  cp -p Include/*.h* ../Include
  cp -p README{,_SPQR}.txt
  cp -p README_SPQR.txt Doc/* ../Doc/SPQR
popd

cp -p UFconfig/UFconfig.h Include

mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}
cp -a Lib/*  $PKG/usr/lib${LIBDIRSUFFIX}
mkdir -p $PKG/usr/include/suitesparse
cp -a Include/* $PKG/usr/include/suitesparse 

find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a README.txt Doc/* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
